<?php
// +-------------+------------------+------+-----+---------+----------------+
// | Field       | Type             | Null | Key | Default | Extra          |
// +-------------+------------------+------+-----+---------+----------------+
// | id          | int(11)          | NO   | PRI | NULL    | auto_increment |
// | upload_type | varchar(10)      | YES  |     |         |                |
// | save_path   | varchar(200)     | YES  |     |         |                |
// | ext         | varchar(10)      | YES  |     |         |                |
// | mine_type   | varchar(30)      | YES  |     |         |                |
// | size        | int(11) unsigned | YES  |     | 0       |                |
// | sha1        | varchar(40)      | YES  |     |         |                |
// | md5         | varchar(40)      | YES  |     |         |                |
// | user_id     | int(11) unsigned | YES  |     | 0       |                |
// | member_id   | int(11) unsigned | YES  |     | 0       |                |
// | create_time | int(11) unsigned | YES  |     | 0       |                |
// | update_time | int(11) unsigned | YES  |     | 0       |                |
// | origin_name | varchar(255)     | YES  |     |         |                |
// +-------------+------------------+------+-----+---------+----------------+
namespace app\common\model;

use think\Model;
use think\Request;
class Upload extends Model
{

    /**
    * 保存上传文件
    * @Author zhanghong
    * @Date   2017-01-03
    * @param  [type]     $file     [description]
    * @param  string     $save_dir [description]
    * @return [type]               [description]
    */
    public function saveFile($file, $upload_type = '', $save_dir = ''){
        // 用户上传时选择的文件名
        $local_dir = 'upload';
        $ds = DIRECTORY_SEPARATOR;

        $origin_name = $file->getInfo('name');
        $info = $file->rule('md5')->move($local_dir );
        $data = [];
        $data["upload_type"] = $upload_type;
        $data["ext"] = $info->getExtension();
        $save_name = $info->getSaveName();
        $data["save_path"] = $ds.$local_dir.$ds.$save_name;
        // 保存的文件名
        // $data["file_name"] = $info->getFilename();
        $data["sha1"] = $info->hash("sha1");
        $data["md5"] = $info->hash("md5");
        $data["mine_type"] = $info->getMime();
        $data["size"] = $info->getSize();
        $data["origin_name"] = $origin_name;
        $this->data($data)->allowField(true)->save();
        $data["id"] = $this->id;
        return $data;
    }

    /**
     * 百度编辑器的列表分页查询方法
     * @param  integer $start [description]
     * @param  integer $size  [description]
     * @return [type]         [description]
     */
    public function ueditorList($start = 0, $size = 20){
        $total_count = $this->count();

        $start = intval($start);
        if($start < 0){
            $start = 0;
        }

        $size = intval($size);
        if($size < 0 || $size > 20){
            $size = 20;
        }

        $select_fields = [
            "save_path AS url",
            "create_time AS mtime",
        ];
        $list = $this->field($select_fields)->order("id DESC")->limit("{$start}, {$size}")->select();

        $res = [
            "state" => "SUCCESS",
            "list" => $list,
            "start" => $start,
            "total" => $total_count,
        ];
        return $res;
    }
}
